package com.todo.event.activity.application.handler.dynamicwords;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.utils.StringUtils;
import com.todo.event.activity.common.enums.EventActivityDynamicWordEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author azhebuxing
 * @date 2025/1/13 16:52
 * @description
 */
@Slf4j
@Component
public class UserGeneralWordsHandle extends AbstractDynamicWordsHandle {

    public final static Set<String> USER_GENERAL_WORDS = new HashSet<>();

    {
        USER_GENERAL_WORDS.add(EventActivityDynamicWordEnum.USER_NIKE_NAME.getCode());
        USER_GENERAL_WORDS.add(EventActivityDynamicWordEnum.SURNAME.getCode());
    }


    @Override
    protected boolean match(List<EventActivityDynamicWordEnum> dynamicWords) {
        for (EventActivityDynamicWordEnum dynamicWord : dynamicWords) {
            if (USER_GENERAL_WORDS.contains(dynamicWord.getCode())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 姓氏相关，由于存在复姓，统一处理为 首姓氏 先生/女士
     * 例如慕容 慕 先生/女士
     *
     * @param dynamicWords
     * @return
     */
    @Override
    protected Map<EventActivityDynamicWordEnum, Object> doHandle(List<EventActivityDynamicWordEnum> dynamicWords) {
        //TODO 这里调用对应服务接口然后构造返回结果即可
        JSONObject json = new JSONObject();
        json.put("userName", "张三");
        json.put("userNickName", "azhebuxing");
        json.put("age", 19);
        json.put("gender", 0);
        json.put("job", "程序员");
        Map<EventActivityDynamicWordEnum, Object> result = new HashMap<>();
        for (EventActivityDynamicWordEnum dynamicWord : dynamicWords) {
            switch (dynamicWord) {
                case USER_NIKE_NAME:
                    result.put(dynamicWord, json.getString("userNickName"));
                    continue;
                case SURNAME:
                    result.put(dynamicWord, buildSurname(json.getString("userName"), json.getInteger("gender")));
                    continue;
            }
        }
        return result;
    }

    private String buildSurname(String userName, Integer gender) {
        if (StringUtils.isBlank(userName) || gender == null) {
            //默认 先生
            return "先生";
        }
        char prefix = userName.charAt(0);
        return gender == 0 ? prefix + "先生" : prefix + "女士";
    }

}
